<template>
  <div class="register_container">
    <div class="register_box">
      <el-form :model="registerForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input autocomplete="off" v-model="registerForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="registerForm.password" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="registerForm.checkPass" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="registerForm.phone"></el-input>
        </el-form-item>
        <el-form-item
          prop="email"
          label="邮箱"
          :rules="[
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
          ]"
        >
          <el-input v-model="registerForm.email"></el-input>
        </el-form-item>
        <!--验证码-->
        <el-form-item label="验证码" prop="verifycode">
          <el-input v-model="registerForm.VerifyCode" placeholder="点击图片更换验证码" style="width: 310px"></el-input>
          <div @click="refreshCode">
            <W-VerifyCode :identifyCode="identifyCode"></W-VerifyCode>
          </div>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="Register">提交</el-button>
          <el-button type="success" @click="resetForm">重置</el-button>
          <el-button type="info" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import VerifyCode from './verifycode'
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.checkPass !== '') {
          this.$refs.registerForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        password: '',
        checkPass: '',
        phone: '',
        email: '',
        VerifyCode: ''
      },
      identifyCodes: '1234567890abcdefjhijklinopqrsduvwxyz',
      identifyCode: '',
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    'W-VerifyCode': VerifyCode
  },
  methods: {
    Register () {
      if (this.registerForm.VerifyCode.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.error('请填写正确验证码')
        this.refreshCode()
        return
      }
      var UserInfo = {
        username: this.registerForm.username,
        password: this.registerForm.password,
        phone: this.registerForm.phone,
        email: this.registerForm.email
      }
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('/user/register', UserInfo)
        console.log(res)
        if (res.code === 201) return this.$message.error('注册失败！')
        else if (res.code === 202) return this.$message.error('该用户名已存在，请换个新的用户名')
        this.$message.success('注册成功,跳转至登陆页面登录!')
        console.log(res)
        this.$router.push('/login')
      })
    },
    resetForm () {
      this.$refs.ruleForm.resetFields()
    },
    back () {
      this.$router.go(-1)
    },
    refreshCode () {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    makeCode (o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
      }
    },
    randomNum (min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    }
  },
  mounted () {
    // 初始化验证码
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  }
}
</script>

<style lang="less" scoped>
.register_container{
  height: 100%;
  background-image: url('../../assets/University.png');
  background-repeat: no-repeat;
  background-size:100% 100%;
}
.register_box{
    width: 450px;
    height: 440px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.demo-ruleForm{
    position: absolute;
    width: 100%;
    top: 20px;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
